package com.haiwei.poi.word.poi;

import com.haiwei.poi.common.HaiweiListUtils;
import com.haiwei.poi.common.nassert.HaiweiAssert;
import com.haiwei.poi.word.loop.HaiweiRowGroup;
import com.haiwei.poi.word.loop.HaiweiRowGroupUtil;
import com.haiwei.poi.word.loop.HaiweiTable;
import com.haiwei.poi.word.loop.HaiweiTableUtil;
import org.apache.poi.xwpf.usermodel.*;

import java.util.Iterator;
import java.util.List;

/**
 * XWPFTable 工具类.
 *
 * @date: 2022-06-26
 * @version: 1.0
 * @author: karry@haiwei.com
 */
public class HaiweiXWPFTableUtil {
    
    /**
     * 解析表格
     * @param tables
     */
    public static void parseTable(List<XWPFTable> tables) {
        for(XWPFTable xwpfTable : tables){
            // 解析模板生成haiweiTable模型
            HaiweiTable haiweiTable = HaiweiTableUtil.getHaiweiTable(xwpfTable);
            
            // 解析haiweiTable模型 + 数据模型
            final List<HaiweiRowGroup> rows = haiweiTable.getRows();
            for(HaiweiRowGroup row : rows){
                HaiweiRowGroupUtil.handleRowGroup(row);
            }
            
            //删除模板行
            HaiweiXWPFTableUtil.removeRow(haiweiTable.getTable(),haiweiTable.getStartIndex(),haiweiTable.getEndIndex());
        }
    }
    
    @Deprecated
    public static void parse(Iterator<XWPFTable> iterator){
        if(iterator == null){
            return;
        }
        while (iterator.hasNext()){
            XWPFTable xwpfTable = iterator.next();
            parse(xwpfTable);
        }
    }
    
    @Deprecated
    public static void parse(List<XWPFTable> tables){
        if(HaiweiListUtils.isEmpty(tables)){
            return;
        }
        for(XWPFTable table : tables){
            parse(table);
        }
    }
    
    @Deprecated
    private static void parse(XWPFTable xwpfTable) {
        if(xwpfTable == null){
            return;
        }
        List<XWPFTableRow> xwpfTableRows = xwpfTable.getRows();
        for(XWPFTableRow row : xwpfTableRows){
            parse(row);
        }
    }
    
    public static void parse(XWPFTableRow row) {
        List<XWPFTableCell> cells = row.getTableCells();
        for(XWPFTableCell cell : cells){
            
            List<XWPFParagraph> paragraphs = cell.getParagraphs();
            HaiweiParagraphUtil.pause(paragraphs);
        }
    }
    
    
    /**
     * 删除数据行
     * @param table
     * @param startIndex
     * @param endIndex
     */
    public static void removeRow(XWPFTable table, int index) {
        HaiweiAssert.assertNotNull("table can not bean null");
        HaiweiAssert.assertTrue("index must less row num",index < table.getNumberOfRows());
    
        table.removeRow(index);
    }
    
    /**
     * 删除数据行
     * @param table
     * @param startIndex
     * @param endIndex
     */
    public static void removeRow(XWPFTable table, int startIndex, int endIndex) {
        HaiweiAssert.assertNotNull("table can not bean null",table);
        HaiweiAssert.assertTrue("end must after start",startIndex < endIndex);
        HaiweiAssert.assertTrue("end index must less row num",endIndex < table.getNumberOfRows());
        
        for(int i = endIndex; i >= startIndex; i--){
            table.removeRow(i);
        }
    }
    
    /**
     * 添加空行并Copy模板行样式
     * 逻辑：每次都在 insertIndex 处插入数据
     * @param table
     * @param templateRow 模板行
     * @param insertIndex 插入行下标
     * @param count 插入数量
     */
    public static void addAndCopyRows(XWPFTable table,XWPFTableRow templateRow, int insertIndex, int count) {
        HaiweiAssert.assertNotNull("table can not bean null",table);
        HaiweiAssert.assertTrue("count must >0",count > 0);
        HaiweiAssert.assertTrue("inserIndex must less row num",insertIndex <= table.getNumberOfRows());
        
        XWPFTableRow targetRow = null;
        while (count-- > 0){
            targetRow = table.insertNewTableRow(insertIndex);
            HaiweiXWPFTableRowUtil.copyTableRow(targetRow,templateRow);
            targetRow = null;
        }
    }
}
